<?php


namespace app\pc\service\base;


use app\admin\model\base\Practise;
use app\admin\model\base\Practise as Question;
use app\admin\model\base\StudentPractise;
use app\BaseService;
use think\response\Json;

class PractiseService extends BaseService
{
    /**
     * 列表
     * @param array $params
     * @return Json
     */
    public function index(array $params): Json
    {
        $where = [];
        $where[] = ['is_release','=',1];
        $where[] = ['is_del','=',0];
        if(isset($params['special_id']) && $params['special_id']){
            $where[] = ['special_id','=',$params['special_id']];
        }
        if(isset($params['name']) && $params['name']){
            $where[] = ['name','like',"%{$params['name']}%"];
        }
        //获取练习
        $practiseList = Practise::where('is_del','=',0)
            ->with(['practise_subject','special'])
            ->where($where)
//            ->where('start_time','<',date('Y-m-d H:i:s',time()))
//            ->where('end_time','>',date('Y-m-d H:i:s',time()))
            ->order('end_time','asc')
            ->paginate(['list_rows' => $params['limit'] ?? 10, 'page' => $params['page'] ?? 1], false)
                ->toArray();
        $practiseListArr = [];
        if(!empty($practiseList['data'])){
            foreach ($practiseList['data'] as $item){
                $subject_num = count($item['practise_subject']);
                $subject_score = 0;
                if(!empty($item['practise_subject'])){
                    foreach ($item['practise_subject'] as $subject){
                        $subject_score+= $subject['weighting'];
                    }
                }
                $practiseListArr[] = [
                    'name'=>$item['name'],
                    'end_time'=>$item['end_time'],
                    'subject_num'=>$subject_num,
                    'subject_score'=>$subject_score,
                    'img_url'=>$item['img_url'],
                    'special_name'=>$item['special']['name'] ?? '',
                    'practise_id'=>$item['id'],
                ];
            }
        }
        $practiseList['data'] = $practiseListArr;
        return jsonReturn(1, $practiseList);
    }

    /**
     * 列表
     * @param array $params
     * @return Json
     */
    public function lists(array $params): Json
    {
        $where = [];
        $where[] = ['sp.student_id','=',PC_USER_ID];
        if(isset($params['name']) && $params['name']){
            $where[] = ['p.name','like','%'.$params['name'].'%'];
        }
        if(isset($params['special_id']) && $params['special_id']){
            $where[] = ['p.special_id','=',$params['special_id']];
        }
        //获取练习
        $arr = [];
        $practiseList = StudentPractise::where('sp.is_del','=',0)
            ->alias('sp')
            ->leftJoin('practise p','sp.practise_id = p.id')
//            ->with(['practise'])
            ->where($where)
            ->order('sp.id','desc')
            ->field('sp.*,p.name,p.start_time as practise_start_time,p.end_time as practise_end_time')
            ->paginate(['list_rows' => $params['limit'] ?? 10, 'page' => $params['page'] ?? 1], false)->toArray();
        if(!empty($practiseList['data'])){
            foreach ($practiseList['data'] as $datum){
                $data = [
                    'name'=>$datum['name'],
                    'start_time'=>$datum['start_time'],
                    'end_time'=>$datum['end_time'] ?? '--',
                    'take_time'=>$datum['take_time'] > 0 ? $datum['take_time'] : '--',
                    'id'=>$datum['id'],
                    'practise_id'=>$datum['practise_id']
                ];
                $arr[] = $data;
            }
        }
        $practiseList['data'] = $arr;
        return jsonReturn(1, $practiseList);
    }

    /**
     * 详情
     * @param array $params
     * @return Json
     */
    public function read(array $params): Json
    {
        $where = ['id'=>$params['id']];
        $info = Question::where($where)->with(['practise_subject'])->find();
        if($info){
            $info = $info->toArray();
            $info['count_score'] = 0;
            if(!empty($info['practise_subject'])){
                foreach ($info['practise_subject'] as $k=>$item){
                    unset($info['practise_subject'][$k]['analysis']);
                    unset($info['practise_subject'][$k]['analysis_desc']);
                    $arr = json_decode($item['content'],true);
                    if($item['type'] == 1 || $item['type'] == 2){
                        if(isset($arr['list']) && !empty($arr['list'])){
                            foreach ($arr['list'] as $kk=>$value){
                                unset($arr['list'][$kk]['is_answer']);
                            }
                        }
                    }
                    $info['count_score']+= $item['weighting'];
                    $info['practise_subject'][$k]['content'] = $arr;
                }
            }
        }
        return jsonReturn(1, $info);
    }
}